var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupontab:['未使用','已使用','已过期'],
    couindex:0,
    avai:false,
  },
  coupontabs:function(e){
    var that = this;
    that.setData({
      couindex:e.currentTarget.dataset.dex
    })
    that.getUsercoupon()
  },
  couinp:function(e){
    var that = this;
    var duihuanma = e.detail.value;
    console.log(duihuanma);
    if(duihuanma > 0){
      that.setData({
        avai : true,
        duihuanma:duihuanma
      })
    }else{
      that.setData({
        avai: false,
        duihuanma:duihuanma
      })
    }
  },
  exchange:function(){
    var that = this;
    console.log(that.data.duihuanma);
    app.util.request({
      'url': 'entry/wxapp/coupon.exchange',
      data: {
        openid: wx.getStorageSync('openid'),
        duihuanma:that.data.duihuanma
      },
      success: function (res) {
        console.log(res)
        if(res.data!='兑换成功!'){
          wx.showToast({
            title: res.data,
            icon:"none"
          })
        }else{
          wx.showToast({
            title: res.data,
          })
          setTimeout(function() {
            that.getUsercoupon()
          }, 1000)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.getUsercoupon()
  },
  getUsercoupon:function(){
    var that = this;
    var couindex = that.data.couindex;
    app.util.request({
      'url': 'entry/wxapp/coupon.userlist',
      data: {
        openid: wx.getStorageSync('openid'),
        couindex:couindex
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          list:res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})